const express = require('express');
const path = require('path');
const app = express();
const port = process.env.TEST_SERVER_PORT || 3000;

// 静态文件服务
app.use(express.static(path.join(__dirname, 'fixtures')));

// API路由
app.post('/api/data', (req, res) => {
  res.json({ status: 'success', data: { id: 1 } });
});

app.get('/api/test', (req, res) => {
  res.json({ message: 'Test response' });
});

app.post('/api/large', (req, res) => {
  const largeData = { data: 'a'.repeat(1024 * 1024) };
  res.json(largeData);
});

// 测试页面
app.get('/test-page.html', (req, res) => {
  res.sendFile(path.join(__dirname, 'fixtures/test-page.html'));
});

// 启动服务器
const server = app.listen(port, () => {
  console.log(`Test server running on port ${port}`);
});

// 导出服务器实例和关闭方法
module.exports = {
  server,
  close: () => server.close()
};